#include <QDate>
#include <QSound>
#include <QTimer>
#include <QFile>
#include "script_thread.h"


static const QString play_code = " \
							command.play = function(str,timeout_ms){ \
								var time = new Date(); \
								var start_time = time.getTime(); \
								if(timeout_ms == undefined){\
									timeout_ms = -1; \
								}\
								command.play_mp3(str);\
								while(1){\
									var current_time = (new Date()).getTime(); \
									if(command.get_play_state() == 0){\
										return 1;\
									}\
									if(timeout_ms != -1 && current_time >= start_time+timeout){ \
										command.print(\"wait timeout\");\
										return 0; \
									}\
								}\
							};\
								";


Script_thread::Script_thread(Command *cmd,QObject *parent) : 
	QThread(parent),
	command(cmd)
{
	con_sem = new QSemaphore(0);	
	js_sem = new QSemaphore(0);
	play_sem = new QSemaphore(0);

	test = 19;
	tstr = "ddde";
	quit_flag = 0;


	QObject::connect(command,&Command::recv_js_code,this,&Script_thread::run_js_code);

	player = new QMediaPlayer();
	connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
	connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(handleError(QMediaPlayer::Error)));
	connect(player, &QMediaPlayer::stateChanged, this, &Script_thread::play_state_changed);

	player->setVolume(100);

}

void Script_thread::run()
{
	qDebug() << "Script_thread start threadID:"<< QThread::currentThreadId() << endl;

	QJSValue value = js.newQObject(this);
	js.globalObject().setProperty("command",value);
	js.evaluate(play_code);

	while(1){
		js_sem->acquire(1);
		QString code;
		code = js_code.first();
		js_code.removeFirst();
		print(code);

		quit_flag = 0;
		QJSValue result = js.evaluate(code);
		if(!result.isNull() && !result.isUndefined()){
			print(result.toString());
	 	}
	}
}

int Script_thread::create_console_object(QString name, QObject *console)
{
	QJSValue value = js.newQObject(console);
	js.globalObject().setProperty(name,value);
	return 0;
}

int Script_thread::run_js_code(QString code)
{
	if(code == "quit"){
		qDebug() << "quit"<<endl;
		quit_flag = 1;
		return 0;
	}
	js_code.append(code);
	js_sem->release();
	return 0;
}

void Script_thread::print(QString str)
{
	command->print_log(str);
}

int Script_thread::play(QString file)
{
	play_sem->tryAcquire();
	player->setMedia(QUrl::fromLocalFile(file));
	player->play();
	play_state = 1;
	play_sem->acquire();
	play_state = 0;
	return 0;
}

int Script_thread::play(QString file,int ms)
{
	play_sem->tryAcquire();
	player->setMedia(QUrl::fromLocalFile(file));
	player->play();
	play_state = 1;
	play_sem->tryAcquire(1,ms);
	play_state = 0;
	return 0;
}

bool Script_thread::sleep(int ms)
{
	//qDebug()<< "sleep threadID:"<<QThread::currentThreadId()<<endl;
	con_sem->tryAcquire(1,ms);
	return true;
}

void Script_thread::positionChanged(qint64 a)
{
	//qDebug() << "positionChanged:"<<a<<endl;
}

void Script_thread::handleError(QMediaPlayer::Error error)
{
	qDebug() <<"player error:"<<error<<endl;
}

void Script_thread::play_state_changed(QMediaPlayer::State state)
{
	qDebug() <<"player state:"<<state<<endl;
	if(state == QMediaPlayer::StoppedState && play_state == 1){
		play_sem->release();
	}
}

int Script_thread::get_play_state()
{
	return play_state;
}

int Script_thread::run_js(QString file)
{
	QFile fd(file);
	fd.open(QFile::ReadOnly);
	QString str = fd.readAll();
	fd.close();
	qDebug() << "str:"<<str<<endl;
	run_js_code(str);
	return 0;
}

bool Script_thread::get_quit(void)
{
	return quit_flag;
}

void Script_thread::quit(void)
{
	quit_flag = 1;
}

